package com.iwuke.dance.http_request;

import android.content.Context;
import android.util.Log;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.Request;
import okhttp3.RequestBody;

/**
 * @author: 魏从响
 * @date: 2025/9/12
 * IO流请求方式
 */
public class ReadbyPost extends Thread {

    /**
     * @description //IO流请求方式   //new ReadbyPost().start();
     * @param
     */
    @Override
    public void run() {
        try {
            URL url = new URL("http://192.168.254.103:8080/login");
            HttpURLConnection huc = (HttpURLConnection) url.openConnection();
            huc.setRequestProperty("encoding", "utf-8");
            huc.setDoInput(true);
            huc.setDoOutput(true);
            huc.setRequestMethod("POST");
            OutputStream os = huc.getOutputStream();
            OutputStreamWriter osw = new OutputStreamWriter(os);
            BufferedWriter bw = new BufferedWriter(osw);
            //"username=admin&password=123"
            bw.write("username=admin&password=123");
            bw.flush();
            InputStream is = huc.getInputStream();
            InputStreamReader isr = new InputStreamReader(is, "utf-8");
            BufferedReader br = new BufferedReader(isr);
            String str;
            StringBuilder sb = new StringBuilder();
            while ((str = br.readLine()) != null) {
                sb.append(str);
            }
            br.close();
            isr.close();
            is.close();
            bw.close();
            osw.close();
            os.close();
            //System.out.println(sb.toString());

            Log.d("++++++++++++++++",sb.toString());

        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            Log.d("++++++++++++++++",e.toString());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            Log.d("++++++++++++++++",e.toString());
        }
    }









    private static Request setForm(Context context, String url, ArrayList<String> pathList, Map<String, String> map) {
        // form 表单形式上传
        MultipartBody.Builder requestBody = new MultipartBody.Builder();
        requestBody.setType(MultipartBody.FORM);
        //pathList是文件路径对应的列表
        if (null != pathList && pathList.size() > 0) {
            for (int i = 0; i < pathList.size(); i++) {
                File file = new File(pathList.get(i));
                if (file != null) {
                    // MediaType.parse() 里面是上传的文件类型。
                    RequestBody body = RequestBody.create(MediaType.parse("image/jpeg"), file);

                    // 参数分别为， 请求key ，文件名称 ， RequestBody
                    requestBody.addFormDataPart("image", file.getName(), body);
                }
            }
        }

        Map<String, String> paramsMap = MapUtil.getMap(context, url, map);
        if (paramsMap != null) {
            for (String key : paramsMap.keySet()) {
                requestBody.addFormDataPart(key, paramsMap.get(key));
            }
        }
        //创建Request对象
        Request request = new Request.Builder().url(url)
                .put(requestBody.build()).build();

        return request;
    }




}

